package com.xie.smfs.event;

import com.xie.smfs.entity.GhostEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.world.World;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;

public class GhostInteractionHandler implements UseEntityCallback {
    @Override
    public ActionResult interact(PlayerEntity player, World world, Hand hand, Entity entity, EntityHitResult hitResult) {
        // 仅在服务端处理交互
        if (world.isClient) {
            return ActionResult.PASS;
        }

        // 检查交互实体是否为普通幽灵
        if (entity instanceof GhostEntity ghost) {
            if (ghost.isDeadlocked() && ghost.hasCoffinNail() && player.getStackInHand(hand).isEmpty()) {
                ItemStack coffinNail = ghost.getCoffinNail();
                ghost.setCoffinNail(null);
                ghost.setSuppressed(false);
                ghost.enableGhostDomain();
                ghost.enableKillingRules();
                if (player.getAbilities().creativeMode) {
                    player.giveItemStack(new ItemStack(coffinNail.getItem(), 1));
                } else {
                    if (!player.getInventory().insertStack(coffinNail)) {
                        entity.dropStack(coffinNail, 0.5f);
                    }
                }
                return ActionResult.SUCCESS;
            }
        }

        return ActionResult.PASS;
    }
}